﻿using UnityEngine;

namespace Super
{
	public class BaseSingle<T> : MonoBehaviour where T : BaseSingle<T>
	{
		public bool isInitd = false;
		public virtual void OnInit() 
		{
			isInitd = true;
		}
		
		private void Awake()
		{
			OnInit();
		}

		/// <summary>
		/// 调用这个方法时，未初始化会调用OnInit方法初始化
		/// </summary>
		public void Init()
		{
			
		}

		/// <summary>
		/// 检测是否销毁
		/// </summary>
		/// <returns></returns>
		public static bool CheckIfNotExist()
		{
			return ins == null;
		}

		private static T ins;
		public static T Ins
		{
			get
			{
				if (ins == null)
					ins = FindObjectOfType(typeof(T)) as T;
				if (ins == null)
				{
					Transform parent = GameUtil.GetOrCreateSceneRoot("ManagerBox", FWConfig.managerBox);
					GameObject go = new GameObject(typeof(T).Name);
					go.transform.SetParent(parent);
					go.transform.position = Vector3.zero;
					ins = go.AddComponent<T>();
				}
				
				if (!ins.isInitd) ins.OnInit();
				
				return ins;
			}
		}
	}

}
